<?php
/**
 * Created by PhpStorm.
 * User: ZhangWei
 * Date: 2018/3/23
 * Time: 9:12
 */

namespace App\Http\Controllers\Api\Open;


use App\Http\Controllers\Api\ApiBase;
use App\Http\Resources\TagCollection;
use App\Models\ScanCode;
use App\Models\Tag;
use Illuminate\Http\Request;

class PublicController extends ApiBase
{
    public function getLoginQrCode()
    {
        ScanCode::query()->where('add_time', '<', time() - 60)->where('user_id', 0)->delete();
        $scan_code = ScanCode::query()->create([
            'key' => str_random(32),
            'user_id' => 0,
            'add_time' => time()
        ]);

        $data['key'] = $scan_code->key;

        $qr_url = config('wx.web_host') . 'qr-login?action=login&key=' . $scan_code->key;

        $data['qr_code'] = \QrCode::format('svg')->size(300)->margin(0)->encoding('UTF-8')->generate($qr_url);
        return $this->success($data);

        /*$app = Factory::miniProgram($this->getWxAppConfig());

        $qr_code = $app->app_code->getUnlimit('/pages/member/login/main');

        return $qr_code;*/


    }


    public function searchTag(Request $request)
    {
        $user = $this->getUser();
        $name = $request->input("name");
        $list = Tag::query()->where('name', 'like', "%$name%")->get();
        $data = TagCollection::make($list);
        return $this->success($data);
    }

}